<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<%@ include file="/include.jsp"%>

<script type="text/javascript">
var menuId = "${menuId}" || window.parent.menuId;
$(function(){
    var $orginfo = $('#orginfo');
    var $datagrid = $('#datagrid');
    var $contextmenu = $('#contextmenu');
    var $addwindow = $('#addwindow');
    var $updatewindow = $('#updatewindow');
    
    var handlers = {
        'view': function() {
            alert("查看");
        },
        
        'add': function() {
            $addwindow.panel('open');
        },
        
        'update': function() {
            $updatewindow.panel('open');
        },
        
        'delete': function() {
            var org = $datagrid.datagrid('getSelected');
            if (org) {
                $.messager.confirm('确认删除?', '删除操作将会删除所有下级，<br/>确认要删除吗?', function(state){
                    if (state){
                        $.getJSON(ctx + '/sys/org/delete', {id: org.id}, function(json) {
                            location.reload();
                        }, 'json');
                    }
                });
            }
        }
    };
    
    $orginfo.panel({
        title: '部门信息',
        iconCls: 'icon-search',
        collapsible: true,
        tools: [{
            iconCls: 'icon-save',
            handler: function() {
                var $form = $orginfo.find('form');
                $form.form({
                    url: $form.attr('action') || ctx + '/sys/dept/update',
                    success: function(jsonResult) {
                        jsonResult = eval('(' + jsonResult + ')');
                        if (jsonResult.success) {
                            location.reload();
                        }
                    }
                }).submit();
            }
        }]
    });
    
    $datagrid.treegrid({
        url: ctx + '/sys/org/children/${dept.orgId}',
        queryParams: {
            tree: true
        },
        title: '下级组织',
        iconCls: 'icon-edit',
        collapsible: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: true,
        selectOnCheck: true,
        remoteSort: false,
        lines: true,
        idField: 'id',
        treeField: 'name',
        loadFilter: function(data, parentId) {
            if (data) {
                for (var i in data) {
                    var item = data[i];
                    if ('post' == item.orgType && item.children != null) {
                        item.state = 'closed';
                    }
                    if (item.children) {
                        arguments.callee(item.children, parentId);
                    }
                }
            }
            return data;
        },
        onClickRow: function(row) {
            $datagrid.treegrid('select', row.id);
        },
        onContextMenu: function(e, row) {
            e.preventDefault();
            $datagrid.datagrid('clearSelections');
            $datagrid.treegrid('select', row.id);
            $contextmenu.menu('show', {
                left: e.pageX - 10,
                top: e.pageY - 10
            });
        },
        columns: [[
            {title: '组织ID', field: 'id', sortable: true, width: 100},
            {title: '组织名称', field: 'name', sortable: true, width: 100},
            {title: '组织类型', field: 'orgType', sortable: true, width: 100},
            {title: '类型描述', field: 'orgTypeDesc', sortable: true, width: 100},
            {title: '排序号', field: 'order', sortable: true, width: 100}
        ]],
        onBeforeLoad: function(param) {
            $.getJSON(ctx + '/sys/oper/list-authorized-opers-by-menuid', {
                menuId: menuId
            }, function(opers) {
                var btns = [];
                var items = [];
                $.each(opers, function(i, oper){
                    if (i > 0) {
                        btns.push('-');
                        items.push({separator:true});
                    }
                    btns.push({
                        id: 'btn-' + oper.id,
                        text: oper.name,
                        iconCls: oper.iconCls,
                        handler: handlers[oper.handler],
                    });
                    items.push({
                        text: oper.name,
                        iconCls: oper.iconCls,
                        onclick: handlers[oper.handler]
                    });
                });
                $datagrid.datagrid("appendToolbarItem", btns);
                
                $.each(items, function(i, item) {
                    $contextmenu.menu('appendItem', item);
                });
            });
        },
        toolbar: []
    });
    
    $contextmenu.menu({}).menu('hide');
    
    $addwindow.dialog({
        title: '添加组织',
        width: 400,
        height: 300,
        collapsible: true,
        closed: true,
        cache: false,
        modal: true,
        href: ctx + '/sys/org/add',
        onBeforeOpen: function() {
            var node = $datagrid.datagrid('getSelected');
            if (node) {
                var params = $addwindow.panel('options').queryParams;
                $.extend(params, {
                    parentType: node.orgType,
                    parentId: node.id
                });
                $addwindow.panel('options').queryParams = params;
            }
        },
        method: 'get',
        buttons: [{
            text: '保存', iconCls: 'icon-save', handler: function() {
                var $form = $addwindow.find('form');
                $form.form({
                    url: $form.attr('action') || ctx + '/sys/org/add',
                    success: function(jsonResult) {
                        jsonResult = eval('(' + jsonResult + ')');
                        if (jsonResult.success) {
                            /* $addwindow.window('close');
                            $datagrid.treegrid('reload'); */
                            location.reload();
                        }
                    }
                }).submit();
            }
        }]
    });
    
    $updatewindow.dialog({
        title: '修改组织',
        width: 400,
        height: 300,
        collapsible: true,
        closed: true,
        cache: false,
        modal: true,
        href: ctx + '/sys/org/update',
        method: 'get',
        onBeforeOpen: function() {
            var org = $datagrid.datagrid('getSelected');
            if (org) {
                var params = $addwindow.panel('options').queryParams;
                $.extend(params, {
                    orgId: org.id
                });
                $updatewindow.panel('options').queryParams = params;
            } else {
                return false;
            }
        },
        buttons: [{
            text: '保存', iconCls: 'icon-save', handler: function() {
                var $form = $updatewindow.find('form');
                $form.form({
                    url: $form.attr('action') || ctx + '/sys/org/update',
                    success: function(jsonResult) {
                        jsonResult = eval('(' + jsonResult + ')');
                        if (jsonResult.success) {
                            /* $updatewindow.window('close');
                            $datagrid.treegrid('reload'); */
                            location.reload();
                        }
                    }
                }).submit();
            }
        }]
    });
    
});
</script>

<title>TSWEB</title>
</head>
<body>
    <div id="orginfo" style="width:100%;">
        <form action="${ctx}/sys/dept/update" method="post">
            <table>
                <tbody>
                    <tr>
                        <td><label for="dept-id">部门ID</label></td>
                        <td><input type="text" id="dept-id" name="id" value="${dept.id}" readonly="readonly"/></td>
                    </tr>
                    <tr>
                        <td><label for="dept-name">部门名称</label></td>
                        <td><input type="text" id="dept-name" name="name" value="${dept.name}"></td>
                    </tr>
                    <tr>
                        <td><label for="dept-orgId">组织ID</label></td>
                        <td><input type="text" id="dept-orgId" name="orgId" value="${dept.orgId}" readonly="readonly"></td>
                    </tr>
                    <tr>
                        <td><label for="dept-parentId">上级组织</label></td>
                        <td><input type="text" id="dept-parentId" name="parentId" value="${dept.parentId}" readonly="readonly"></td>
                    </tr>
                    <tr>
                        <td><label for="dept-order">排序号</label></td>
                        <td><input type="text" id="dept-order" name="order" value="${dept.order}" readonly="readonly"></td>
                    </tr>
                </tbody>
            </table>
        </form>
    </div>
    <div style="margin:5px 0;"></div>    
    <div id="datagrid" style="width:100%;"></div>
    <div id="contextmenu"></div>
    <div id="addwindow"></div>
    <div id="updatewindow"></div>
</body>
</html>